from django.core.exceptions import ValidationError
from django.shortcuts import render

# Create your views here.
from rest_framework import mixins, viewsets
from rest_framework.exceptions import MethodNotAllowed
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from rider.models import Rider
from rider.serializers import RiderSerializer


class RiderView(mixins.RetrieveModelMixin,
                mixins.DestroyModelMixin,
                mixins.UpdateModelMixin,
                mixins.CreateModelMixin,
                viewsets.GenericViewSet):
    """
        retrieve:
            骑手详情
        update:
            修改骑手
        create:
            骑手招募
        destroy:
            骑手注销
    """
    queryset = Rider.objects.all()
    serializer_class = RiderSerializer

    def get_queryset(self):
        return Rider.objects.filter(users=self.request.user)

    def perform_create(self, serializer):
        # users = serializer.validated_data.get("users")
        rider = serializer.save()
        users = rider.users
        users.role_type = 2
        users.save()

    def perform_destroy(self, instance):
        users = instance.users
        users.role_type = 1
        users.save()
        instance.delete()

